package com.twu.biblioteca.Libraries;

import com.twu.biblioteca.Book;

import java.util.*;

public class BooksLibrary implements Library {
    
    HashMap<String , Book> booksCollection = new HashMap<String, Book>();
    HashMap<String , BookStatus> booksReservationStatus = new HashMap<String, BookStatus>();

    private enum BookStatus{
        NAVAIL,AVAIL
    }


    public BooksLibrary() {
       loadItems();
    }

    public void addBook(Book book) {
        booksCollection.put( book.getIsbnNo(), book );
        booksReservationStatus.put(book.getIsbnNo(), BookStatus.AVAIL);
    }
    @Override
    public void loadItems() {
       Book book;

       book= new Book("Head First Java","123");
        addBook(book);
       book = new Book("Design Patterns","345");
        addBook(book);
       book = new Book("Refactoring Code","678");
        addBook(book);
       book = new Book("Atlas Shrugged","879");
        addBook(book);
       book = new Book("The Kite Runner", "987");
        addBook(book);
    }

    @Override
    public Vector<String> getAllItemDetails() {
        Vector<String> booksDetails=new Vector<String>();

        Iterator<Book> booksIterator = booksCollection.values().iterator();

        while( booksIterator.hasNext()) {
            Book book = booksIterator.next();
            booksDetails.add(book.getIsbnNo() + " " + book.getTitle());

        }
        return booksDetails;

    }

    public boolean reserveBook(String isbnNo){

        if(booksReservationStatus.get(isbnNo) == BookStatus.AVAIL){
            toggleStatus(isbnNo);
            System.out.println("Thank You!Enjoy the book.");
            return true;
        }

        else{
            System.out.println("Sorry we don't have the book yet.");
            return false;
        }
    }


    public final void toggleStatus(String isbnNo){
        if(booksReservationStatus.get(isbnNo) == BookStatus.AVAIL)
            booksReservationStatus.put(isbnNo , BookStatus.NAVAIL);

        else
          booksReservationStatus.put(isbnNo , BookStatus.AVAIL);
    }
}
